package com.qckj.amz.domain.spapi.feeds.home.PricingFeedXml;

import com.qckj.amz.domain.*;
import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import com.qckj.amz.utils.constants.AmzConstants;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 创建价格xml
 * @author luchuan
 * @version v1.0
 * @date 2022/11/20 0020
 *
 **/

public class CreatePricingFeedXml {


    /**
     *
     * @param sellerId      卖家记号
     * @param productInit   产品常用属性
     * @param listProduct   产品信息
     * @param configMarketplace
     * @return
     */
    public String getPricingFeedXml(String sellerId, ProductInit productInit, List<Map<String, Object>> listProduct, ConfigMarketplace configMarketplace) {

        if (listProduct==null && listProduct.isEmpty()){
            return null;
        }

        BigDecimal currencyRate = configMarketplace.getCurrencyRate();// 币种汇率
        String currencyCode = configMarketplace.getCurrencyCode();// 币种

        ArrayList<Message> messageList = new ArrayList<>();//封装多个Message，即多个sku产品，无论是有无变体或多个不同的产品
        for (Map<String, Object> mapProduct: listProduct) {
            ProductFmlBasic basicObj = (ProductFmlBasic)mapProduct.get("basicObj");
            ProductFmlDesc descObj = (ProductFmlDesc)mapProduct.get("descObj");
            List<ProductFmlRlat> rlatList = (List<ProductFmlRlat>)mapProduct.get("rlatList");

            BigDecimal marketspacePriceCoefficient = descObj.getMarketspacePriceCoefficient();//

            //封装多个变体
            for (ProductFmlRlat rlat:rlatList) {


                // 站点活动售价 = 人民币售价 * 站点售价系数 * 币种汇率
                BigDecimal salePrice_CNY = rlat.getSalePrice() == null ? new BigDecimal(0) : rlat.getSalePrice();
                String saleFromDate = rlat.getSaleFromDate() == null ? "" : rlat.getSaleFromDate().toString();
                String saleEndDate = rlat.getSaleEndDate() == null ? "" : rlat.getSaleEndDate().toString();

                Sale sale = new Sale();
                if (salePrice_CNY.compareTo(new BigDecimal(0))==1 && !saleFromDate.equals("") && !saleEndDate.equals("")){

                    BigDecimal salePrice_currency = salePrice_CNY.multiply(marketspacePriceCoefficient).multiply(currencyRate).setScale(2,BigDecimal.ROUND_HALF_UP);
                    // 根据不同站点的币种，设置售价
                    SalePrice salePrice = new SalePrice(currencyCode,String.valueOf(salePrice_currency));

                    sale.setStartDate(saleFromDate);
                    sale.setEndDate(saleEndDate);
                    sale.setSalePrice(salePrice);
                }

                // 站点售价 = 人民币售价 * 站点售价系数 * 币种汇率
                BigDecimal standardPrice_CNY = rlat.getStandardPrice();//人民币售价
                BigDecimal standardPrice_currency = standardPrice_CNY.multiply(marketspacePriceCoefficient).multiply(currencyRate).setScale(2,BigDecimal.ROUND_HALF_UP);
                // 根据不同站点的币种，设置售价
                StandardPrice standardPrice = new StandardPrice(currencyCode, String.valueOf(standardPrice_currency));

                Price price = new Price();
                price.setSKU(rlat.getItemSku());
                price.setStandardPrice(standardPrice);
                price.setSale(sale);


                /**
                 * 二级/Message,此节点为List对象，可以包含主体、变体
                 */
                Message message = new Message();
                message.setMessageID(basicObj.getId());
                message.setOperationType(productInit.getUpdateDelete());//Update、delete
                message.setPrice(price);
                messageList.add(message);

            }

        }

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier(sellerId);

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope amazonEnvelope = new AmazonEnvelope();
        amazonEnvelope.setHeader(header);
        amazonEnvelope.setMessageType("Price");//Product、Price
        amazonEnvelope.setMessage(messageList);

        /**
         * Javabean对象转换xml字符串
         */
        String xmlStr = AmzCommon.JavabeanToXmlStr(AmzConstants.FEED_TYPE_PRICING_FEED, amazonEnvelope);

        return xmlStr;
    }

}
